package com.ebeitech.library.util;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Set;

/**
 * Created by hrzong on 2017/5/9.
 */

public class StringUtil {

    public static boolean isEmpty(String a) {
        if (a == null || a.equals("") || a.trim().equals("") || a.equals("null")) {
            return true;
        }
        return false;
    }

    public static String getDefaultString(String string){
        if(isEmpty(string)){
            return "";
        }
        return string;
    }

    public static String getDefaultString(String string, String sDefault){
        if(isEmpty(string)){
            return sDefault;
        }
        return string;
    }


    public static String convertToString(Object obj, String separater) {
        String retStr = "";
        if (obj instanceof Set) {
            Set<String> idSet = (Set<String>) obj;
            boolean isFirstEntry = true;
            for (String id : idSet) {
                if (!isFirstEntry) {
                    retStr = retStr + "," + separater + id + separater;
                } else {
                    retStr = separater + id + separater;
                    isFirstEntry = false;
                }
            }
        } else if (obj instanceof List) {
            List<String> idList = (List<String>) obj;
            for (int i = 0; i < idList.size(); i++) {
                String id = idList.get(i);
                if (i != 0) {
                    if (!retStr.contains(id)){
                        retStr = retStr + "," + separater + id + separater;
                    }
                } else {
                    if (!retStr.contains(id)){
                        retStr = separater + id + separater;
                    }
                }
            }
        } else if (obj instanceof String[]) {
            String[] strArr = (String[]) obj;
            boolean isFirstEntry = true;
            for (String id : strArr) {
                if (!isFirstEntry) {
                    retStr = retStr + "," + separater + id + separater;
                } else {
                    retStr = separater + id + separater;
                    isFirstEntry = false;
                }
            }
        }
        return retStr;
    }
    public static boolean isStringNullOrEmpty(String str) {
        return str == null || "".equals(str.trim())
                || TextUtils.isEmpty(str.trim());
    }

    public static String encodeMessageToString(String message, String key) {
        if (isStringNullOrEmpty(message)) {
            return "";
        }
        try {
            JSONObject object = new JSONObject(message);
            return object.optString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 格式化 （保留count位）
     *
     * @param str
     * @param count
     * @return
     */
    public static String formatWithDecimal(String str, int count) {
        String format = "0.";
        for (int i = 0; i < count; i++) {
            format = format + "0";
        }
        if (isStringNullOrEmpty(str) || "null".equals(str)) {
            return format;
        } else {
            return new DecimalFormat(format).format(Double.valueOf(str));
        }
    }

    public static double parseDouble(String str) {
        double result = 0;
        try {
            if (!isEmpty(str)) {
                result = Double.valueOf(str);
            }
        } catch (Exception e) {}
        return result;
    }

}
